﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.User;

public partial class admin_user_Message : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

         if (!Page.IsPostBack)
         {
             ClearForm();
             if (Request.Params["MessageId"] != null)
             {
                 Int64 _messageid;
                 if (Int64.TryParse(Request.Params["MessageId"].ToString(), out _messageid))
                 {
                     ShowMessage(_messageid);
                 }
             }
         }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/User/ViewMessage.aspx");
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearForm();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Message _message = new Message();
        MessageDB _messagedb = new MessageDB();
        
        if (!Page.IsValid)
        {
            lblMessage.Text = "Page is not valid!";
            return;
        }

        _message.MessageId = Convert.ToInt64(lblMessageId.Text);
        _message.UserTypeId = Convert.ToInt32(ddlUserType.Text);
        _message.MessageType = ddlMessageType.Text;
        _message.ErrorCode = txtErrorCode.Text;
        _message.Tittle = txtTittle.Text;
        _message.Header = txtHeader.Text;
        _message.Body = txtBody.Text;
        _message.Footer = txtFooter.Text;
        _message.Status = chkStatus.Checked;
        _message.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

        _message.MessageId = _messagedb.Insert(_message);

        if (_message.MessageId > 0)
        {
            ClearForm();
            if (btnSubmit.Text.ToUpper() == "UPDATE")
            {
                lblMessage.Text = "Update successfully!";
            }
            else
            {
                lblMessage.Text = "Insert successfully!";
            }
        }
        else
        {
            if (btnSubmit.Text.ToUpper() == "UPDATE")
            {
                lblMessage.Text = "Update failed!";
            }
            else
            {
                lblMessage.Text = "Insert failed!";
            }
        }
    }

    private void ClearForm()
    {
        lblMessage.Text = "";
        lblMessageId.Text = "0";
        ddlUserType.Text = "-1";
        ddlMessageType.Text = "ALL";
        txtErrorCode.Text = "";
        txtTittle.Text = "";
        txtHeader.Text = "";
        txtBody.Text = "";
        txtFooter.Text = "";
        chkStatus.Checked = true;
        btnSubmit.Text = "Submit";
    }

    private void ShowMessage(Int64 MessageId)
    {
        Message _message = new Message();
        MessageDB _messagedb = new MessageDB();
        _message = _messagedb.GetMessage(MessageId);
        if (_message != null)
        {
            lblMessageId.Text = _message.MessageId.ToString();
            ddlUserType.Text = _message.UserTypeId.ToString();
            ddlMessageType.Text = _message.MessageType;
            txtErrorCode.Text = _message.ErrorCode;
            txtTittle.Text = _message.Tittle;
            txtHeader.Text = _message.Header;
            txtBody.Text = _message.Body;
            txtFooter.Text = _message.Footer;
            chkStatus.Checked = _message.Status;
            btnSubmit.Text = "Update";
        }
    }

}
